﻿using System;
using System.Net;
using System.Web.Http;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Spotted.Web.Data;
using Spotted.WebApi.Controllers;
using Spotted.WebApi.Models;

namespace Spotted.WebApi.Tests.ControllerTests
{
    [TestClass]
    public class UserTests : TestBase
    {
        [TestMethod]
        public void can_extract_user_controller_from_kernel()
        {
            var controller = GetFromKernel<UserController>("User");
            Assert.AreNotEqual(null, controller);
        }

        [TestMethod]
        public void receive_successful_create_user_result_when_creating_valid_new_user()
        {
            var controller = GetFromKernel<UserController>("User");
            var viewModel = new CreateUserViewModel
                {
                    EmailAddress = "test",
                    FirstName = "test",
                    LastName = "test",
                    Password = "test"
                };

            var result = controller.Post(viewModel);
            Assert.AreEqual(ResultCode.Success, result.ResultCode);
            Assert.AreEqual(false, string.IsNullOrEmpty(result.UserToken));
            Assert.AreNotEqual(Guid.Empty, Guid.Parse(result.UserToken));
        }

        [TestMethod]
        public void receive_httpresponseexception_with_http_code_406_when_viewmodel_has_model_errors()
        {
            try
            {
                var controller = GetFromKernel<UserController>("User");
                var viewModel = new CreateUserViewModel();

                // add the model error we would get from model binding
                controller.ModelState.AddModelError("LastName", "Last Name is required");
                controller.Post(viewModel);

                Assert.Fail("No exception was raised as was expected");
            }
            catch (HttpResponseException responseEx)
            {
                Assert.AreEqual(HttpStatusCode.NotAcceptable, responseEx.Response.StatusCode);
                Assert.AreNotEqual(true, string.IsNullOrEmpty(responseEx.Response.ReasonPhrase));
            }
        }

        [TestMethod]
        public void receive_httpresponseexception_with_http_code_409_when_email_is_already_registered()
        {
            try
            {
                var controller = GetFromKernel<UserController>("User");
                var viewModel = new CreateUserViewModel
                    {
                        EmailAddress = "duplicate@example.com",
                        FirstName = "my first name",
                        LastName = "my last name",
                        Password = "my password"
                    };

                controller.Post(viewModel);
                Assert.Fail("No exception was thrown where one was expected");
            }
            catch (HttpResponseException htex)
            {
                var response = htex.Response;
                Assert.AreEqual(HttpStatusCode.Conflict, response.StatusCode);
                Assert.AreEqual(false, string.IsNullOrEmpty(response.ReasonPhrase));
            }
        }
    }
}